﻿///==========================================================================
/// @file: Form1.cs
/// @description: 
///
/// Copyright © 2013, Simnix Software
/// Author: Simon Nixon
/// $Id$
/// $URL$
///==========================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OFC;


namespace OscilloscopeFrequency
{
    public partial class Main : Form
    {
        double dFrequency;          //variable to hold frequency
        double dPeriod;             //veriable to hold period
        double dTimePerDivision;    //time per devistion
        double dNumberOfDivisions;  //number of divisions for a cycle

        public Main()
        {
            InitializeComponent();
            // Display application name and version on the title bar
            this.Text = "OFC  v" + ProductVersion.ToString();

            //Initialises all the selection boxes to there default states
            txtTimeDiv.Text = "1.0";
            cbxSeconds.Text = "s";
            txtNumberDivisions.Text = "1.0";
            txtFrequency.Text = "";
            cbxHertz.Text = "Hz";
            txtPeriod.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //put the values into the variables
            dTimePerDivision = double.Parse(txtTimeDiv.Text);
            dNumberOfDivisions = double.Parse(txtNumberDivisions.Text);

            dPeriod = (dNumberOfDivisions * dTimePerDivision);
            txtPeriod.Text = string.Format("{0:0.00}", dPeriod) + " " + cbxSeconds.Text;

            //conversion for milliseconds or microseconds
            switch (cbxSeconds.Text)
            {
                case "ms":
                    dTimePerDivision /= 1000;
                    break;
                case "μs":
                    dTimePerDivision /= 1000000;
                    break;
                case "ns":
                    dTimePerDivision /= 1000000000;
                    break;
            }

            //calculate the frequency
            dFrequency = (1.0 / dTimePerDivision) / dNumberOfDivisions;
            

            switch (cbxHertz.Text)
            {
                case "Hz":
                    //txtFrequency.Text = dFrequency.ToString();
                    txtFrequency.Text = string.Format("{0:0.####}", dFrequency);
                    break;                
                case "KHz":
                    dFrequency /= 1000;
                    //txtFrequency.Text = dFrequency.ToString();
                    txtFrequency.Text = string.Format("{0:0.####}", dFrequency);
                    if (txtFrequency.Text == "0")
                    {
                        MessageBox.Show("Use lower frequency \"Hz\"");
                    }
                    break;
                case "MHz":
                    dFrequency /= 1000000;
                    //txtFrequency.Text = dFrequency.ToString();
                    txtFrequency.Text = string.Format("{0:0.####}", dFrequency);
                    if (txtFrequency.Text == "0")
                    {
                        MessageBox.Show("Use lower frequency \"Hz or KHz\"");
                    }
                    break;
            }            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            //Resets all the selection boxes to there default states
            txtTimeDiv.Text = "1.0";
            cbxSeconds.Text = "s";
            txtNumberDivisions.Text = "1.0";
            txtFrequency.Text = "";
            cbxHertz.Text = "Hz";
            txtPeriod.Text = "";
        }

        private void linkLabelAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //Load and show the frmAbout form
            frmAbout About = new frmAbout();
            About.ShowDialog();
            About.Top = this.Top;
            About.Left = (this.Left + this.Width);
        }
    }
}